package game.util;

import java.util.LinkedList;

public class Initiative {
	
	private LinkedList<InitiativeCharacterPair> list = null;
	private int index;
	
	public Initiative() {
		list = new LinkedList<InitiativeCharacterPair>();
		index = -1;
	}
	
	public void add(int value, long unitId) {
		InitiativeCharacterPair entry = new InitiativeCharacterPair(value, unitId);
		if (list.size() == 0)
			list.add(entry);
		else {
			boolean added = false;
			for (int i = 0; i < list.size() && !added; i++) {
				if (list.get(i).getInitiative() < value) {
					list.add(i, entry);
					added = true;
				}
			}
			if (!added)
				list.add(entry);
		}
	}
		
	public void remove(long unitId) {
		for (InitiativeCharacterPair u : list) {
			if (u.getCharId() == unitId)
				list.remove(u);
		}
	}
	
	public void removeFirst() {
		list.removeFirst();
	}
	
	public void removeLast() {
		list.removeLast();
	}
	
	public void clear() {
		list.clear();
		index = -1;
	}
	
	public int indexOf(String unit) {
		return list.indexOf(unit);
	}
	
	public long next() {
		if (list.size() == 0)
			return 0;
		else {
			if (++index == list.size())
				index = 0;
			return list.get(index).getCharId();
		}
	}

}
